package com.gxgy.service.impl;

import java.util.List;
import java.util.Random;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gxgy.base.BaseAllMapper;
import com.gxgy.entity.Book;
import com.gxgy.mapper.BookMapper;
import com.gxgy.service.BookSerivce;
import com.gxgy.uilt.BaseUtil;
import com.gxgy.uilt.Id;
import com.gxgy.uilt.IfVersionException;
import com.gxgy.uilt.MD5Util;
import com.gxgy.uilt.RandomId;
/**
 * @description: 
 * @author: Qin Fengang
 * @date: 2021-11-18
 */
@Service
public class BookSerivceImpll extends BaseAllMapper implements BookSerivce {

	
	/**
	 * @description: 图书表 新增
	 * @author: 邓 忠柠
	 * @date: 2021-11-18
	 */
	@Transactional
	@Override
	public Integer addBook(String bookName, String bookType,
			String bookAuthor, String bookDescribe,
			String enerateTime,Model model) {
		if (bookName.isEmpty()) {
			model.addAttribute("msg","图书名称不能为空");
			throw new IfVersionException("图书名称不能为空");
		}
		if (bookType.isEmpty()) {
			model.addAttribute("msg","图书类型不能为空");
			throw new IfVersionException("图书类型不能为空");
		}
		if (bookAuthor.isEmpty()) {
			model.addAttribute("msg","图书作者不能为空");
			throw new IfVersionException("图书作者不能为空");
		}
		if (bookDescribe.isEmpty()) {
			model.addAttribute("msg","图书描述不能为空");
			throw new IfVersionException("图书描述不能为空");
		}
		if (enerateTime.isEmpty()) {
			model.addAttribute("msg","录入日期不能为空");
			throw new IfVersionException("录入日期不能为空");
		}
		String id = BaseUtil.getBaseId();
		Book book = new Book();
		book.setId(Id.book_+id);
		book.setBookName(bookName);
		book.setBookType(bookType);
		book.setBookAuthor(bookAuthor);
		book.setBookDescribe(bookDescribe);
		book.setEnerateTime(enerateTime);
		return bookMapper.insert(book);
	}

	
	/**
	 * @description: 图书表 查询所有
	 * @author: 邓忠柠
	 * @date: 2021-11-18
	 */
	@Override
	public List<Book> queryAllBook() {

		return bookMapper.selectList(null);
	}


	@Transactional
	@Override
	public Integer updateBook(String id, String bookName, String bookType, String bookAuthor, String bookDescribe,
			String enerateTime, Model model) {
		if (id.isEmpty()) {
			model.addAttribute("msg","图书编号不能为空");
			throw new IfVersionException("图书编号不能为空");
		}
		if (bookName.isEmpty()) {
			model.addAttribute("msg","图书名称不能为空");
			throw new IfVersionException("图书名称不能为空");
		}
		if (bookType.isEmpty()) {
			model.addAttribute("msg","图书类型不能为空");
			throw new IfVersionException("图书类型不能为空");
		}
		if (bookAuthor.isEmpty()) {
			model.addAttribute("msg","图书作者不能为空");
			throw new IfVersionException("图书作者不能为空");
		}
		if (bookDescribe.isEmpty()) {
			model.addAttribute("msg","图书描述不能为空");
			throw new IfVersionException("图书描述不能为空");
		}
		if (enerateTime.isEmpty()) {
			model.addAttribute("msg","录入日期不能为空");
			throw new IfVersionException("录入日期不能为空");
		}
		Book book = new Book();
		book.setId(id);
		book.setBookName(bookName);
		book.setBookType(bookType);
		book.setBookAuthor(bookAuthor);
		book.setBookDescribe(bookDescribe);
		book.setEnerateTime(enerateTime);
		UpdateWrapper<Book> wrapper = new UpdateWrapper<Book>()
				.eq(Book.ID, book.getId());
		return bookMapper.update(book, wrapper);
	}

	/**
	 * @description: 图书表 查询通过id
	 * @author: 邓忠柠
	 * @date: 2021-11-18
	 */
	@Override
	public Book queryBookById(String id, Model model) {
		if (id.isEmpty()) {
			model.addAttribute("msg","图书编号不能为空");
			throw new IfVersionException("图书编号不能为空");
		}
		QueryWrapper<Book> wrapper = new QueryWrapper<Book>()
				.eq(Book.ID, id);
		return bookMapper.selectOne(wrapper);
	}

}
